package com.bike.bikeserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bike.bikeserver.common.core.domain.CommonResult;
import com.bike.bikeserver.domain.BikeDispatch;
import com.bike.bikeserver.domain.BikeRequest;
import com.bike.bikeserver.domain.WXUser;
import com.bike.bikeserver.domain.vo.BikeDispatchHandleVo;
import com.bike.bikeserver.domain.vo.HistoryInfoVo;
import com.bike.bikeserver.mapper.BikeDispatchMapper;
import com.bike.bikeserver.mapper.BikeRequestMapper;
import com.bike.bikeserver.mapper.WXUserMapper;
import com.bike.bikeserver.service.IBikeDispatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.bike.bikeserver.constant.Constants.*;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/7/10 15:33
 */
@Service
public class BikeDispatchServiceImpl implements IBikeDispatchService
{
    @Autowired
    BikeDispatchMapper bikeDispatchMapper;
    @Autowired
    BikeRequestMapper bikeRequestMapper;
    @Autowired
    WXUserMapper wxUserMapper;

    @Override
    public CommonResult getUnhandledBikeDispatches()
    {
        LambdaQueryWrapper<BikeDispatch> wrapper_bikeDispatch = new LambdaQueryWrapper<>();
        wrapper_bikeDispatch.eq(BikeDispatch::getDispatchState, UNHANDLED_STATE);
        List<BikeDispatch> bikeDispatches = bikeDispatchMapper.selectList(wrapper_bikeDispatch);
        for (BikeDispatch bikeDispatch : bikeDispatches)
        {
            bikeDispatch.setDispatchTime(bikeDispatch.getCreateTime());
        }
        return CommonResult.success(bikeDispatches);
    }

    @Override
    @Transactional
    public CommonResult acceptDispatchById(BikeDispatchHandleVo bikeDispatchHandleVo)
    {
        // 首先更新调度状态为已完成
        BikeDispatch bikeDispatch = bikeDispatchMapper.selectById(bikeDispatchHandleVo.getId());
        bikeDispatch.setDispatchReceiver(bikeDispatchHandleVo.getNickName());
        bikeDispatch.setDispatchState(COMPLETED);
        bikeDispatchMapper.updateById(bikeDispatch);
        // 然后更新请求状态为已完成
        LambdaQueryWrapper<BikeRequest> wrapper_bikeRequest = new LambdaQueryWrapper<>();
        wrapper_bikeRequest.eq(BikeRequest::getRequestState, UNHANDLED_STATE);
        wrapper_bikeRequest.eq(BikeRequest::getRequestApplier, bikeDispatch.getDispatchApplier());
        wrapper_bikeRequest.eq(BikeRequest::getRequestType, BIKE_DISPATCH);
        BikeRequest bikeRequest = bikeRequestMapper.selectOne(wrapper_bikeRequest);
        bikeRequest.setRequestState(COMPLETED);
        // todo 显示时从前端获取这个调度信息，前端从后台获取
        bikeRequest.setRequestResult("80辆单车已完成调度");/**/
        bikeRequestMapper.updateById(bikeRequest);
        // 更新用户的积分数
        LambdaQueryWrapper<WXUser> wrapper_wxuser = new LambdaQueryWrapper<>();
        wrapper_wxuser.eq(WXUser::getNickName, bikeDispatchHandleVo.getNickName());
        WXUser wxUser = wxUserMapper.selectOne(wrapper_wxuser);
        wxUser.setRewardsNum(wxUser.getRewardsNum() + DISPATCH_SCORE);
        wxUserMapper.updateById(wxUser);
        return CommonResult.success("调度成功！同时您获得" + DISPATCH_SCORE + "积分");
    }

    @Override
    @Transactional
    public CommonResult rejectDispatchById(BikeDispatchHandleVo bikeDispatchHandleVo)
    {
        // 首先更新调度状态为已拒绝
        BikeDispatch bikeDispatch = bikeDispatchMapper.selectById(bikeDispatchHandleVo.getId());
        bikeDispatch.setDispatchReceiver(bikeDispatchHandleVo.getNickName());
        bikeDispatch.setDispatchState(REJECTED);
        bikeDispatchMapper.updateById(bikeDispatch);
        // 然后更新请求状态为已拒绝
        LambdaQueryWrapper<BikeRequest> wrapper_bikeRequest = new LambdaQueryWrapper<>();
        wrapper_bikeRequest.eq(BikeRequest::getRequestState, UNHANDLED_STATE);
        wrapper_bikeRequest.eq(BikeRequest::getRequestApplier, bikeDispatch.getDispatchApplier());
        wrapper_bikeRequest.eq(BikeRequest::getRequestType, BIKE_DISPATCH);
        BikeRequest bikeRequest = bikeRequestMapper.selectOne(wrapper_bikeRequest);
        bikeRequest.setRequestState(REJECTED);
        // todo 可能需要改
        bikeRequest.setRequestResult("因某些原因，被工作人员拒绝执行");/**/
        bikeRequestMapper.updateById(bikeRequest);
        return CommonResult.success();
    }

    @Override
    public CommonResult getDispatchListByNickName(String nickName)
    {
        LambdaQueryWrapper<BikeDispatch> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BikeDispatch::getDispatchApplier, nickName);
        List<BikeDispatch> bikeDispatches = bikeDispatchMapper.selectList(wrapper);
        // 转换成historyInfo列表格式
        List<HistoryInfoVo> historyInfoVoList = new ArrayList<>();
        for (BikeDispatch bikeDispatch : bikeDispatches)
        {
            HistoryInfoVo historyInfoVo = new HistoryInfoVo();
            historyInfoVo.setTime(bikeDispatch.getUpdateTime());
            historyInfoVo.setName("处理地区：" + bikeDispatch.getZoneName());
            /**/
            // todo
            historyInfoVo.setInfo("调度详情：" + bikeDispatch.getDispatchInfo());//后期与api有关，注意了，后期改，可能需要链表查询
            historyInfoVo.setState(bikeDispatch.getDispatchState());
            historyInfoVoList.add(historyInfoVo);
        }
        return CommonResult.success(historyInfoVoList);
    }

    @Override
    public CommonResult getBikeDispatchList()
    {
        List<BikeDispatch> bikeDispatches = bikeDispatchMapper.selectList(null);
        return CommonResult.success(bikeDispatches);
    }

    @Override
    public CommonResult getBackDispatchByNickNameBack(String dispatchApplier)
    {
        LambdaQueryWrapper<BikeDispatch> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BikeDispatch::getDispatchApplier, dispatchApplier);
        List<BikeDispatch> bikeDispatches = bikeDispatchMapper.selectList(wrapper);
        return CommonResult.success("查询成功！为您找到" + bikeDispatches.size() + "条记录！", bikeDispatches);
    }
}
